<?php

namespace app\admin\controller;

use app\common\controller\AdminBase;

class User extends AdminBase
{
    protected function _initialize()
    {
        parent::_initialize();
        if ($this->request->isGet()) {
            $this->assign('institute', model('user')->field('institute')->group('institute')->select());
            $this->assign('jobTitle', model('user')->field('job_title')->group('job_title')->select());
        }
    }

    public function index()
    {
        $param = $this->request->param();
        $where = [];
        $whereOr = [];
        if (isset($param['name'])) {
            $whereOr['user_name'] = ['like', "%" . $param['name'] . "%"];
            $whereOr['email'] = ['like', "%" . $param['name'] . "%"];
            $whereOr['phone'] = ['like', "%" . $param['name'] . "%"];
        }
        if (isset($param['institute'])) {
            $where['institute'] = $param['institute'];
        }
        if (isset($param['job_title'])) {
            $where['job_title'] = $param['job_title'];
        }
        if (isset($param['createFrom']) && isset($param['createTo'])) {
            $param['createFrom'] .= " 00:00:00";
            $param['createTo'] .= " 23:59:59";
            $where['u.create_time'] = array('between', array($param['createFrom'], $param['createTo']));
        } else {
            if (isset($param['createFrom'])) {
                $param['createFrom'] .= " 00:00:00";
                $where['u.create_time'] = array('egt', $param['createFrom']);
            }
            if (isset($param['createTo'])) {
                $param['createTo'] .= " 23:59:59";
                $where['u.create_time'] = array('elt', $param['createTo']);
            }
        }
        if (isset($param['useFrom']) && isset($param['useTo'])) {
            $param['useFrom'] .= " 00:00:00";
            $param['useTo'] .= " 23:59:59";
            $where['use_time'] = array('between', array($param['useFrom'], $param['useTo']));
        } else {
            if (isset($param['useFrom'])) {
                $param['useFrom'] .= " 00:00:00";
                $where['update_time'] = array('egt', $param['useFrom']);
            }
            if (isset($param['useTo'])) {
                $param['useTo'] .= " 23:59:59";
                $where['use_time'] = array('elt', $param['useTo']);
            }
        }
        $orderColumn = 'use_time';
        if (isset($param['orderColumn'])) {
            $orderColumn = $param['orderColumn'];
        }
        $orderType = 'desc';
        if (isset($param['orderType'])) {
            $orderType = $param['orderType'];
        }
        $list = db('user')->alias('u')
            ->join('user_resource ur', 'ur.user_id = u.id', 'left')
            ->field('u.*, COUNT(ur.id) as count')
            ->whereOr(function ($query) use ($whereOr) {
                $query->whereOr($whereOr);
            })
            ->where(function ($query) use ($where) {
                $query->where($where);
            })
            ->group('u.id')
            ->order($orderColumn, $orderType)
            ->paginate(config('page_number'), false, ['query' => $param]);
        return $this->fetch('index', ['list' => $list]);
    }

    public function edit()
    {
        if ($this->request->isPost()) {
            $param = $this->request->param();
            if ($this->update('user', $param, false, input('_verify', true)) === true) {
                insert_admin_log('修改了名片');
                $this->success('修改成功', url('admin/user/index'));
            } else {
                $this->error($this->errorMsg);
            }
        }
        return $this->fetch('save', ['data' => model('user')->where('id', input('id'))->find()]);
    }

    public function logTotal()
    {
        list($y, $m, $d) = explode('-', date('Y-m-d'));
        $todayStart = date('Y-m-d H:i:s', mktime(0, 0, 0, $m, $d-1, $y));
        $todayEnd = date('Y-m-d H:i:s', mktime(23, 59, 59, $m, $d-1, $y));

        $type1Total = model('user_log')->where('type', '=', '1')->count();
        $type1Today = model('user_log')->where('type', '=', '1')->whereBetween('create_time', array($todayStart, $todayEnd))->count();

        $type2Total = model('user_log')->where('type', '=', '2')->count();
        $type2Today = model('user_log')->where('type', '=', '2')->whereBetween('create_time', array($todayStart, $todayEnd))->count();

        $type3Total = model('user_log')->where('type', '=', '3')->count();
        $type3Today = model('user_log')->where('type', '=', '3')->whereBetween('create_time', array($todayStart, $todayEnd))->count();

        $type4Total = model('user_log')->where('type', '=', '4')->count();
        $type4Today = model('user_log')->where('type', '=', '4')->whereBetween('create_time', array($todayStart, $todayEnd))->count();

        $obj['type1Total'] = $type1Total;
        $obj['type1Today'] = $type1Today;
        $obj['type2Total'] = $type2Total;
        $obj['type2Today'] = $type2Today;
        $obj['type3Total'] = $type3Total;
        $obj['type3Today'] = $type3Today;
        $obj['type4Total'] = $type4Total;
        $obj['type4Today'] = $type4Today;
        return $this->result($obj, 1);
    }

    public function logChartIndex()
    {
        $param = $this->request->param();
        $where = [];
        if (isset($param['type'])) {
            $where['type'] = $param['type'];
        } else {
            $where['type'] = 1;
        }
        $dts = model('user_log')->field('left(create_time, 10) as dt, count(1) as count')
            ->where($where)
            ->group('dt')->order('dt')->select()->toArray();
        $obj['dts'] = $dts;
        return $this->result($obj, 1);
    }

    public function top10Log()
    {
        $param = $this->request->param();
        $orderColumn = "viewCount";
        if (isset($param['orderColumn'])) {
            $orderColumn = $param['orderColumn'];
        }
        $list = db('user')->alias('u')
            ->join('(select user_id,count(1) as viewCount from user_log ul where ul.type = 1 GROUP BY ul.user_id) as a', 'a.user_id = u.id', 'left')
            ->join('(select user_id,count(1) as receiveCount from user_log ul where ul.type = 3 GROUP BY ul.user_id) as b', 'b.user_id = u.id', 'left')
            ->join('(select user_id,count(1) as shareCount from user_log ul where ul.type = 4 GROUP BY ul.user_id) as c', 'c.user_id = u.id', 'left')
            ->field('u.user_name, u.institute, u.create_time, a.viewCount, b.receiveCount, c.shareCount')
            ->order($orderColumn, 'desc')
            ->limit(10)
            ->select();
        return $this->result($list);
    }

    public function log()
    {
        $list = db('user_log')->alias('ug')
            ->join('user u', 'ug.user_id = u.id', 'left')
            ->field('ug.id as id, u.user_name, u.institute, u.create_time, ug.create_time as update_time, ug.type')
            ->order('ug.create_time desc')->paginate(config('page_number'));
        return $this->fetch('log', ['list' => $list]);
    }

}
